package com.bandit.dao;

import com.bandit.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bandit
 * @create 2022/2/22 23:43
 */
@Data
public class UserDao {

    /**
     * 模拟数据库
     */
    private List<User> userList = new ArrayList<>();

    UserDao(){
        Collections.addAll(userList,new User(1,"张三",20,"北京xx路","一个活泼的男孩"),
                new User(2,"李四",21,"北京xx路","一个开朗的男孩"),
                new User(3,"王五",22,"北京xx路","一个帅气的男孩"));
    }

    public static UserDao getUserDao(){
        return new UserDao();
    }

    public List<User> getUserList(){
        return userList;
    }

    public User getUserById(int id){
        List<User> users = userList.stream().filter(user -> user.getId() == id).collect(Collectors.toList());
        if (users.size()==0) return null;
        else return users.get(0);
    }

}
